﻿//Table:S_JiaRiXX

#region Entity

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Entity;
using Platform.Configuration;
using Platform.Data;

namespace Entity
{
    /// <summary>
    ///     节假日记录表
    /// </summary>
    [Serializable]
    public class S_JiaRiXX
    {
        private DateTime? m_d_jiari;
        private int m_id;

        /// <summary>
        ///     自增量
        /// </summary>
        public int ID
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        ///     节假日
        /// </summary>
        public DateTime? D_JiaRi
        {
            get { return m_d_jiari; }
            set { m_d_jiari = value; }
        }
    }
}

#endregion

#region EntityProcess

namespace BLL
{
    public abstract class S_JiaRiXXProcess
    {
        #region Field

        protected string conn;

        #endregion

        #region Construction

        public S_JiaRiXXProcess()
        {
            conn = ConfigHelper.PlatformConnString;
        }

        #endregion

        #region public

        public bool InsertOrUpdate(S_JiaRiXX S_JiaRiXX, DbTransaction Transaction)
        {
            if (S_JiaRiXX.ID == 0)
                InsertS_JiaRiXX(S_JiaRiXX, Transaction);
            else
                UpdateS_JiaRiXX(S_JiaRiXX, Transaction);
            return S_JiaRiXX.ID == 0 ? false : true;
        }

        public bool DeleteS_JiaRiXX(int ID, DbTransaction Transaction)
        {
            var spName = "_DeleteS_JiaRiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public bool DeleteS_JiaRiXXesDynamic(string WhereClause, DbTransaction Transaction)
        {
            var spName = "_DeleteS_JiaRiXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = WhereClause;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public DataTable LeftJoin<EntityName>(string On, string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.Text,
                    "select * from S_JiaRiXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.Text,
                    "select * from S_JiaRiXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public S_JiaRiXX SelectS_JiaRiXX(int ID, DbTransaction Transaction)
        {
            var spName = "_SelectS_JiaRiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            SqlDataReader rd = null;
            S_JiaRiXX _S_JiaRiXX = null;
            if (Transaction == null)
                rd = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                rd = SqlHelper.ExecuteReader((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (rd.Read())
                _S_JiaRiXX = PopulateS_JiaRiXX(rd);
            if (rd != null)
                rd.Close();
            return _S_JiaRiXX;
        }

        public DataTable SelectS_JiaRiXXesAll(DbTransaction Transaction)
        {
            var spName = "_SelectS_JiaRiXXesAll";
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_JiaRiXXesDynamic(string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            var spName = "_SelectS_JiaRiXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_JiaRiXXesBySerialNumberID(int SerialNumberID, string OrderByExpression,
            DbTransaction Transaction)
        {
            return SelectS_JiaRiXXesDynamic("SerialnumberID=" + SerialNumberID, OrderByExpression, Transaction);
        }

        #endregion

        #region Private

        private int InsertS_JiaRiXX(S_JiaRiXX S_JiaRiXX, DbTransaction Transaction)
        {
            var spName = "_InsertS_JiaRiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_JiaRiXX.D_JiaRi;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            S_JiaRiXX.ID = (int) storedParams[1].Value;
            return result;
        }

        private bool UpdateS_JiaRiXX(S_JiaRiXX S_JiaRiXX, DbTransaction Transaction)
        {
            var spName = "_UpdateS_JiaRiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_JiaRiXX.ID;
            storedParams[1].Value = S_JiaRiXX.D_JiaRi;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        private S_JiaRiXX PopulateS_JiaRiXX(IDataReader dr)
        {
            var S_JiaRiXX = new S_JiaRiXX();
            S_JiaRiXX.ID = dr["ID"] == DBNull.Value ? 0 : (int) dr["ID"];
            S_JiaRiXX.D_JiaRi = dr["D_JiaRi"] == DBNull.Value ? null : (DateTime?) dr["D_JiaRi"];
            return S_JiaRiXX;
        }

        private void AddRowNumber(DataTable dt)
        {
            dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
            for (var i = 0; i < dt.Rows.Count; i++)
                dt.Rows[i][dt.Columns.Count - 1] = i + 1;
        }

        #endregion
    }
}

#endregion